package Yahtzee;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Scanner;

/**
 * @author Ken Veenstra
 * CommandParser.java
 */

public class CommandParser
{
	Scanner inputConsole = new Scanner(System.in);
	String line;
	Game game;
	
	public CommandParser(Game game)
	{
		this.game = game;
		printFile("newGame");
	}
	private String getConsoleLine()
	{
		return inputConsole.nextLine();
	}
	public void readConsole()
	{
		line = getConsoleLine();
		mainMenu();
		while(inputConsole.hasNext())
		{
			line = getConsoleLine();
			mainMenu();
		}
		inputConsole.close();
	}
	private void mainMenu()
	{
		if(line.equals("1"))
		{
			speluitleg();
		} 
		else if(line.equals("2"))
		{
			playerLijst();
		}
		else if(line.startsWith("3"))
		{
			addPlayer();
		}
		else if(line.startsWith("4"))
		{
			deletePlayer();
		}
		else if(line.startsWith("5"))
		{
			playGame();
		}
		else if(line.equals("9"))
		{
			exit();
		}
		else
		{
			System.out.println("No function found for this key, try help [1]");
		} 
	}
	public Scanner getScanner()
	{
		return inputConsole;
	}
	public void speluitleg()
	{
		printFile("speluitleg");
	}
	public void playerLijst()
	{
		game.printPlayers();
	}
	public void addPlayer()
	{
		
	}
	public void deletePlayer()
	{
		
	}
	public void playGame()
	{
		
	}
	public void nextRoll()
	{
		
	}
	public void endGame()
	{
		
	}
	public void exit()
	{
		printFile("exit");
	}
	public void printFile(String file)
	{ //internet helped me a little bit..
		try{
		    FileInputStream fstream = new FileInputStream("Yahtzee/"+file+".ytz");	// Open the file that is the first, command line parameter
		    DataInputStream in = new DataInputStream(fstream);						// Get the object of DataInputStream
		    BufferedReader br = new BufferedReader(new InputStreamReader(in));
		    String strLine;

		    while ((strLine = br.readLine()) != null)  								//Read File Line By Line
		    {
		    	System.out.println (strLine);										//Print the content on the console
		    }																		
		    in.close();																//Close the input stream
		    }catch (Exception e)
		    {																		
		    	System.err.println("Error: " + e.getMessage());						//Catch exception if any
		    }
	}
}
